package 配置文件持久化;

import java.io.*;
import java.util.*;

public class 配置文件持久化
{

    // 1. DataSourceConfig 类（需实现 Serializable）
    public static class DataSourceConfig implements Serializable {
        private String name;
        private String url;
        private String username;
        private String password;

        public DataSourceConfig(String name, String url, String username, String password) {
            this.name = name;
            this.url = url;
            this.username = username;
            this.password = password;
        }

        public String getName() {
            return name;
        }

        public String getUrl() {
            return url;
        }

        public String getUsername() {
            return username;
        }

        public String getPassword() {
            return password;
        }

        @Override
        public String toString() {
            return "DataSourceConfig{" +
                    "name='" + name + '\'' +
                    ", url='" + url + '\'' +
                    ", username='" + username + '\'' +
                    '}';
        }
    }

    // 2. SystemConfig 类（实现 Serializable）
    public static class SystemConfig implements Serializable {
        private Map<String, String> properties;
        private List<DataSourceConfig> dataSources;

        public SystemConfig(Map<String, String> properties, List<DataSourceConfig> dataSources) {
            this.properties = properties;
            this.dataSources = dataSources;
        }

        public Map<String, String> getProperties() {
            return properties;
        }

        public List<DataSourceConfig> getDataSources() {
            return dataSources;
        }

        @Override
        public String toString() {
            return "SystemConfig{\n" +
                    "properties=" + properties + ",\n" +
                    "dataSources=" + dataSources + "\n" +
                    '}';
        }
    }

    // 3. 主程序逻辑
    public static void main(String[] args) {
        String filePath = "system_config.ser";

        try {
            // 创建配置数据
            Map<String, String> props = new HashMap<>();
            props.put("timeout", "30s");
            props.put("retry", "3");

            List<DataSourceConfig> dsList = new ArrayList<>();
            dsList.add(new DataSourceConfig("mainDB", "jdbc:mysql://localhost:3306/main", "root", "pass123"));
            dsList.add(new DataSourceConfig("backupDB", "jdbc:mysql://localhost:3306/backup", "admin", "backupPass"));

            SystemConfig config = new SystemConfig(props, dsList);

            // 保存配置到文件
            saveConfig(config, filePath);
            System.out.println("? 配置已保存到文件：" + filePath);

            // 清除原对象引用（模拟重启或新会话）
            config = null;

            // 从文件加载配置
            SystemConfig loadedConfig = loadConfig(filePath);
            System.out.println("? 配置已从文件加载：\n");

            // 输出配置内容验证
            System.out.println(loadedConfig);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 4. 保存配置到文件
    public static void saveConfig(SystemConfig config, String filePath) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(filePath);
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(config);
        }
    }

    // 5. 从文件加载配置
    public static SystemConfig loadConfig(String filePath)
        throws IOException, ClassNotFoundException {
        try (FileInputStream fis = new FileInputStream(filePath);
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            return (SystemConfig) ois.readObject();
        }
    }
}